package com.epoint.bookmanagement.action.borrowinfo;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collector;
import java.util.stream.Collectors;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.epoint.bookmanagement.bizlogic.bookinfo.BookInfoService;
import com.epoint.bookmanagement.bizlogic.bookinfo.domain.BookInfo;
import com.epoint.bookmanagement.bizlogic.borrowinfo.BorrowInfoService;
import com.epoint.bookmanagement.bizlogic.borrowinfo.domain.BorrowInfo;
import com.epoint.bookmanagement.utils.RequestUtils;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;

/**
 * Servlet implementation class BorrowInfoListAction
 */
@WebServlet("/borrowinfolistaction")
public class BorrowInfoListAction extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	BorrowInfoService borrowInfoService = new BorrowInfoService();
	BookInfoService bookInfoService = new BookInfoService();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public BorrowInfoListAction() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
//		response.getWriter().append("Served at: ").append(request.getContextPath());
		String requestMethod = request.getParameter("method");
		String json = RequestUtils.reflectInvokMethod(requestMethod, request,"com.epoint.bookmanagement.action.borrowinfo.BorrowInfoListAction");
		response.getWriter().write(json);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	
	/**
	 * 查询列表数据，带有排序字段和排序方向
	 * @param pageIndex 页号
	 * @param pageSize 页面大小
	 * @param sortField 排序字段
	 * @param sortOrder 排序方向
	 * @param request 请求
	 * @return 结果
	 */
	public String queryBorrowInfo(HttpServletRequest request) {
		String pageIndex = request.getParameter("pageIndex");
		String pageSize = request.getParameter("pageSize");
		String sortField = request.getParameter("sortField");
		String sortOrder = request.getParameter("sortOrder");
		
		List<BorrowInfo> list = borrowInfoService.queryBorrowInfo(Integer.parseInt(pageIndex), Integer.parseInt(pageSize), sortField, sortOrder);
		List<String> BookIds = list.stream().map(BorrowInfo::getBookid).collect(Collectors.toList());
		// 根据bookid获取图书信息Map<bookid, BookInfo>
		Map<String, BookInfo> map = bookInfoService.queryBookInfoByIds(BookIds); 
		
		List<Map<String, Object>> dataList = new ArrayList<>(list.size());
		
		for(BorrowInfo o : list) {
			Map<String, Object> m = new HashMap<>(7);
			m.put("borrowid", o.getBorrowid());
			m.put("bookid", o.getBookid());
			m.put("bookname", map.get(o.getBookid()).getBookName());
			m.put("borrower", o.getBorrower());
			m.put("phone", o.getPhone());
			m.put("borrowtime", o.getBorrowtime());
			m.put("returntime", o.getReturntime());
			dataList.add(m);
		}
		
		int total = borrowInfoService.queryBorrowInfo(null, null , sortField, sortOrder).size();
		Map<String, Object> result = new HashMap<>(2);
		result.put("total", total);
		result.put("data", dataList);
		return JSONObject.toJSONStringWithDateFormat(result, "yyyy-MM-dd HH:mm:ss");
	}
	
	/**
	 * 生成borrowId
	 * @param request 请求
	 * @return id
	 */
	public String generalBorrowId(HttpServletRequest request) {
		String borrowId = borrowInfoService.generalBorrowId();
		if (StrUtil.isBlank(borrowId)) {
			borrowId = "Borrow" + DateUtil.format(DateUtil.date(), "yyyy") + "0001";
		}
		return JSONObject.toJSONString(borrowId);
	}
	
	/**
	 * 获取所有图书信息
	 * @param request 请求
	 * @return 结果
	 */
	public String queryBookinfoByKeyWords(HttpServletRequest request) {
		List<BookInfo> result = bookInfoService.queryBookinfoByKeyWords(null, null, null, null);
		return JSONObject.toJSONString(result);
	}
	

}
